<%--                                                                       --%>
<%-- Copyright (c) 2005 BroadVision, Inc.  All rights reserved.            --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>


<%@ page import =  "java.util.HashMap,
                    org.apache.commons.beanutils.PropertyUtils,
                    org.apache.struts.validator.DynaValidatorForm"
%>

<%@ page import="java.util.ResourceBundle" %>
<%@ page import="java.util.MissingResourceException" %>

<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<script language="javascript" src="<%=request.getContextPath()%>/mt/js/userPopup.js"></script>



<%
   ResourceBundle resources = ResourceBundle.getBundle("com.broadvision.manage.common.tools.action.ApplicationResources",request.getLocale());
   %>

<%
  java.util.HashMap map = new java.util.HashMap();
  DynaValidatorForm mtUserGroupForm = (DynaValidatorForm) request.getAttribute("mtUserGroupForm");
  map.put("userId", (Long) PropertyUtils.getSimpleProperty(mtUserGroupForm, "userId"));
  pageContext.setAttribute("pagetabParam", map, PageContext.PAGE_SCOPE);
%>

<c:set var="removeImg">
<img src="<%= request.getContextPath() %>/mc/images/action/icon_remove.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.remove" bundle="mt.messages"/>" title="<bean-el:message key="image.alt.remove" bundle="mt.messages"/>">
</c:set>

<html-el:form action="/mt/userGroupRemove.do">

<html-el:hidden name="mtUserGroupForm" property="userId"/>
<html-el:hidden property="oneGroupFQN"/>
<html-el:hidden property="oneOnly"/>

<html-el:hidden property="fqn"/>

<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">

      <div class="pageTitle">
            <bean-el:message key="mt.user.view.page.title" bundle="mt.messages"/>:
            <c:out value="${mtUserGroupView.map.formattedName}"/>
      </div>

      <div class="errorMessage"><html-el:errors bundle="mt.messages"/></div>
      <%@include file="/mc/common/pagetab.jsp" %>

      <!-- end of PageTab -->

      <div class="pageSubtitle">
        <span class="left">
          <bean-el:message key="mt.user.group.groups" bundle="mt.messages"/>
        </span>

        <span class="right">
            <input type="button" class="titleButton" name="addGroupBtn" onclick="return doGroupPopup('<html-el:rewrite page="/mt/groupPopup.do"/>', 'mtUserGroupForm','fqn','name');" value='<bean-el:message key="button.selectGroups" bundle="mt.messages"/>'>
        </span>
      </div>

      <c:if test="${not empty mtUserGroupView.map.groupList}">

        <div class="globalButtonWrap">
          <input type="button" name="selectAllGroups" class="globalButton" onClick="check(this.form.groupFQNs, this);" value='<bean-el:message key="button.selectAll" bundle="mt.messages"/>'>
          <input type="button" name="removeGroups" class="globalButton" onclick="if (isAnyChecked(this.form.groupFQNs, '<bean-el:message key="mt.group.alert.atLeastOneGroup" bundle="mt.messages"/>', '')) doGroupsRemoveAction(this.form);" value='<bean-el:message key="button.remove" bundle="mt.messages"/>'>
        </div>
      </c:if>

      <!--start main content area here-->
      <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Category" width="100%">
        <tr class="listHeader">
          <th id="Checkboxes" align="center" width="5%"></th>
          <th id="SubCategoryName" align="left" width="25%">
            <bean-el:message key="mt.user.group.name" bundle="mt.messages"/>
          </th>
          <th id="Actions" align="left"  width="15%" nowrap>
            <bean-el:message key="mt.header.actions" bundle="mt.messages"/>
          </th>
        </tr>


        <c:choose>
          <c:when test="${not empty mtUserGroupView.map.groupList}">

            <select name="newGroupFQNs" multiple="multiple" size="0" style="visibility: hidden; display: none;">
              <c:forEach var="oneGroup" items="${mtUserGroupView.map.groupList}" varStatus="status">
                <c:if test="${oneGroup.map.fqnLink == true}">
                <c:set var="existingGroupFQNs" >
				                <c:out value="${existingGroupFQNs}${oneGroup.map.fqn}$#@!" escapeXml="false"/>
				 </c:set>
                  <option value="<c:out value="${oneGroup.map.fqn}" />" selected><c:out value="${oneGroup.map.fqn}" /></option>
                </c:if>
              </c:forEach>
            </select>
            <select name="noLinkGroupFQNs" multiple="multiple" size="0" style="visibility: hidden; display: none;">
              <c:forEach var="oneGroup" items="${mtUserGroupView.map.groupList}" varStatus="status">
                <c:if test="${oneGroup.map.fqnLink == false}">
                  <option value="<c:out value="${oneGroup.map.fqn}" />" selected><c:out value="${oneGroup.map.fqn}" /></option>
                </c:if>
              </c:forEach>
            </select>

            <c:forEach var="oneGroup" items="${mtUserGroupView.map.groupList}" varStatus="status">
              <html-el:hidden name="oneGroup" property="fqn" indexed="true"/>
              <c:choose>
                <c:when test="${(status.count + 1) % 2 == 0}">
	<tr class="listEven">
                </c:when>
  	        <c:otherwise>
	<tr class="listOdd">
	        </c:otherwise>
              </c:choose>

          <td headers="Checkboxes" align="center">
            <c:if test="${oneGroup.map.fqnLink == true}">
              <input type="checkbox" name="groupFQNs" onclick="update(document.mtUserGroupForm.groupFQNs, this.form.selectAllGroups);"
                value='<c:out value="${oneGroup.map.fqn}"/>'/>
            </c:if>
          </td>
          <td headers="SubCategoryName">
            <c:choose>
              <c:when test="${oneGroup.map.fqnLink == true}">
                <c:url var="viewUrl" value="/mt/groupView.do">
                  <c:param name="groupFQN" value="${oneGroup.map.fqn}"/>
                </c:url>
                <a href='<c:out value="${viewUrl}"/>'>


                     <c:set var="oneGrMap">
				                    <c:out value="${oneGroup.map.name}"/>
				                    </c:set>
				                    <%
				  				  			    String result="";
				  				                              String mykey="";
				  				  			     String sa=(String) pageContext.getAttribute("oneGrMap");
				  				  			    sa=sa.trim();
				  				                              String[] str=sa.split("/");

         // Groups in LDAP will have no "/" so the length will always be 1

             if (str.length == 1)
             result = str[0]; 
				  				                              for(int z=0;z<str.length;z++){
				  				                               String noa=str[z];
				  				  			     String nam=noa.replaceAll(" ","_");
				  				                              try{ mykey=resources.getString("mt.group.select."+nam);
				  				                              } catch(MissingResourceException  ex){
				  				                                 mykey=noa;
				  				                              }
				  				                              if(z>0){
				  				  			     mykey="/"+mykey;
				  				                               result=result+mykey;
				  				                               }
				  				  			    }
				  				  			    out.print(result);
				  		    %>

                </a>
              </c:when>
              <c:otherwise>
                <c:out value="${oneGroup.map.name}"/>
              </c:otherwise>
            </c:choose>
          </td>
          <td nowrap align="left" headers="Actions">
            <c:if test="${oneGroup.map.fqnLink == true}">
              <a href='#' onclick="JavaScript:doOneGroupRemoveAction(document.mtUserGroupForm, '<c:out value="${status.count - 1}"/>');">
                <c:out value='${removeImg}' escapeXml="false"/></a>
            </c:if>
          </td>
        </tr>

            </c:forEach>
          </c:when>
          <c:otherwise>
            <select name="newGroupFQNs" multiple="multiple" size="0" style="visibility: hidden; display: none;">
            </select>
            <select name="noLinkGroupFQNs" multiple="multiple" size="0" style="visibility: hidden; display: none;">
            </select>
              <tr class="listOdd">
                <td colspan=7 align="center">
                  <bean-el:message key="mt.user.group.noGroup" bundle="mt.messages"/>
                </td>
              </tr>
          </c:otherwise>
        </c:choose>
      </table>
      <!--end main content area here-->
    </td>
  </tr>
</table>

</html-el:form>

<script language="JavaScript">

<%@ include file="/mc/common/jsutils.jsp" %>

  function findField(theForm, fieldName) {
    var elements = theForm.elements;
    var numElements = elements.length;
    for (var elementIndex = 0; elementIndex < numElements; elementIndex++) {
      var elementName = elements[elementIndex].name;
      if (elementName == fieldName) {
        return elements[elementIndex];
      }
    }
    return null;
  }

  function doOneGroupRemoveAction(theForm, i) {
    var removeMessage = '<bean-el:message key="mt.user.group.confirm.remove" bundle="mt.messages"/>';
    if (confirm(removeMessage)) {
      var fieldName = 'oneGroup[' + i + '].fqn';
      var field = findField(theForm, fieldName);
      theForm.oneGroupFQN.value=field.value;
      theForm.oneOnly.value = true;
      theForm.action = '<html-el:rewrite page="/mt/userGroupRemove.do" />';
      theForm.submit();
      return true;
    }
    else {
      return false;
    }
  }

  function doGroupsRemoveAction(theForm) {
    var removeMessage = '<bean-el:message key="mt.user.group.confirm.remove" bundle="mt.messages"/>';
    if (confirm(removeMessage)) {
      theForm.oneOnly.value = false;
      theForm.action = '<html-el:rewrite page="/mt/userGroupRemove.do" />';
      theForm.submit();
      return true;
    }
    else {
      return false;
    }
  }

  //
  // Call back function
  //
  function setNewGroups(ids, names) {
    document.mtUserGroupForm.newGroupFQNs.length = 0;
    var index = 0;
    for (index = 0; index < ids.length; index++) {
      var option = new Option(ids[index], ids[index], true, true);
      document.mtUserGroupForm.newGroupFQNs.options[index] = option;
    }
    for (var index2 = 0; index2 < document.mtUserGroupForm.noLinkGroupFQNs.length; index++, index2++) {
      var text = document.mtUserGroupForm.noLinkGroupFQNs.options[index2].text;
      var value = document.mtUserGroupForm.noLinkGroupFQNs.options[index2].value;
      var option = new Option(text, value, true, true);
      document.mtUserGroupForm.newGroupFQNs.options[index] = option;
    }
    document.mtUserGroupForm.action = '<html-el:rewrite page="/mt/userGroupSave.do"/>';
    document.mtUserGroupForm.submit();
  }

  function doGroupPopup(theForm) {
     <c:url value="/mt/groupMultiPopup.do" var="popUpUrl"  >
 		<c:param name="callback" value="setNewGroups"/>
 		<c:param name="parentFormName" value="abc"/>
 		<c:param name="minSelection" value="0"/>
 		<c:param name="multiSelect" value="true"/>
 		<c:param name="recursiveCheck" value="true"/>
 		<c:param name="existingGroupFQNs" value="${existingGroupFQNs}"/>
 	</c:url>

     var popupUrl = '<c:out value="${popUpUrl}" escapeXml="false"/>';
     var windowFeatures = 'width=600,height=600,scrollbars=yes,resizable=yes';
     var popupWindow = window.open(popupUrl, 'GroupPopup', windowFeatures);
     if (popupWindow != null) {
       if (popupWindow.opener == null) {
         popupWindow.opener = self;
       }
     }
     popupWindow.focus();
  }

</script>
